package com.eagle.gennewdata.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.CRC32;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import com.eagle.gennewdata.untl.GloblasFun;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public final class NewDaoFactory {

	public static String m_strLink = WebSetting.strLink;

	public static String m_strLogo = WebSetting.strLogoString;

	public static String m_strAuthor = WebSetting.strDefaultCategory;

	public static List<NewItem> m_arrNewList = new ArrayList<NewItem>();

	private static final PersistenceManagerFactory pmfInstance = JDOHelper
			.getPersistenceManagerFactory("transactions-optional");

	private NewDaoFactory() {
	}

	public static PersistenceManagerFactory get() {
		return pmfInstance;
	}

	public static void close() {
		PersistenceManager pm = pmfInstance.getPersistenceManager();
		pm.close();
	}

	public static void insertAndUpdateNewData(NewItem itemAdd) {
		long nCrc = GloblasFun.getChecksumValue(new CRC32(), itemAdd.getLink());
		PersistenceManager pm = null;
		try {
			pm = pmfInstance.getPersistenceManager();
			itemAdd.setId(nCrc);
			Key k = KeyFactory.createKey(NewItem.class.getSimpleName(), nCrc);
			itemAdd.setKey(k);
			pm.getObjectById(NewItem.class, k);
		} catch (Exception e) {
			// TODO: handle exception
			itemAdd.setId(nCrc);
			itemAdd.setContentEncoded(GloblasFun.getContentHtml(
					itemAdd.getLink(), itemAdd.getNewTitle()));
			if (itemAdd.getAuthor().equalsIgnoreCase("cnBeta.COM"))
				GloblasFun.insertNewitemLogo(itemAdd);
			NewItem item = pm.makePersistent(itemAdd);
			itemAdd.setContentEncoded(item.getContentEncoded());
			itemAdd.setLogo(item.getLogo());
		} finally {
			pm.close();
		}

		if (m_arrNewList.size() > 60)
			for (int i = 0; i > 10; i++)
				m_arrNewList.remove(0);

	}
	
	public static int getRecordSize() {
		int nRet = 0;
		PersistenceManager pm = get().getPersistenceManager();
		Query query = null;

		try {
			query = pm.newQuery(NewItem.class);
			query.setOrdering("inputDate asc");
			List<NewItem> resultsTmp = extracted(query);
			nRet = resultsTmp.size();
		} finally {
			query.closeAll();
		}
		return nRet;
	}

	public static void delmoreRecord() {
		PersistenceManager pm = get().getPersistenceManager();;
		Query query = null;

		try {
			query = pm.newQuery(NewItem.class);
			query.setOrdering("inputDate asc");
			List<NewItem> resultsTmp = extracted(query);
			if (resultsTmp.size() > 100) {
				int nCount = 0;
				for (NewItem item : resultsTmp) {
					if (nCount++ == 20) {
						pm.deletePersistent(item);
					}
				}
			}
		} finally {
			query.closeAll();
		}
	}

	public static NewItem queryNewItemByID(long nID) {
		NewItem itemReturnItem = null;
		PersistenceManager pm = null;
		try {
			pm = pmfInstance.getPersistenceManager();
			Key k = KeyFactory.createKey(NewItem.class.getSimpleName(), nID);
			itemReturnItem = pm.getObjectById(NewItem.class, k).cloneItem();
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			pm.close();
		}
		return itemReturnItem;
	}

	public static List<NewItem> queryNewItemsByTitle(String strTitle,
			int start, int end) {
		List<NewItem> results = null;
		PersistenceManager pm = null;
		try {
			pm = pmfInstance.getPersistenceManager();
			Query query = pm.newQuery(NewItem.class);
			query.setOrdering("inputDate desc");
			query.setRange(start, end);
			List<NewItem> resultsTmp = extracted(query);
			results = new ArrayList<NewItem>();
			for (NewItem item : resultsTmp) {
				results.add(item.cloneItem());
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.print(e.getMessage());
		} finally {
			pm.close();
		}
		return results;
	}

	@SuppressWarnings("unchecked")
	private static List<NewItem> extracted(Query query) {
		return (List<NewItem>) query.execute();
	}

	public static void deleteNewData() {
		Date date = new Date();
		long lDate = date.getTime() - 3600 * 21 * 1000;
		PersistenceManager pm = null;
		try {
			pm = pmfInstance.getPersistenceManager();
			Query query = pm.newQuery(NewItem.class);
			List<NewItem> results = extracted(query);
			if (results.iterator().hasNext()) {
				for (NewItem e : results) {
					if (e.getInputDate() > lDate)
						pm.deletePersistent(e);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.print(e.getMessage());
		} finally {
			pm.close();
		}
	}

}